/*
 * RBFKeyNvm.h
 *
 *  Created on: 2023年7月14日
 *      Author: fjx
 */

#ifndef MAC_RBFKEYNVM_H_
#define MAC_RBFKEYNVM_H_

#include "RBFCommon.h"
#include "string.h"

#define DEBUG_MODE_KEY
#define xCONFIG_LOG_RBF_KEY_NVM                      //打印key nvm api 日志



/*
 * 如果NVM没有存默认的key,读取该key
 * 该key可以用于测试阶段,可以保证在工厂没有烧录正式key之前可以正常运行(产测测试用,也可以用于demo演示使用)
 *
 *
 */
#define DEFAULTKEY_1_VALUE_DEFALUT                   {\
                                                     0x11, 0x22, 0x33, 0x44, \
                                                     0x55, 0x66, 0x77, 0x88, \
                                                     0xAA, 0xBB, 0xCC, 0xDD, \
                                                     0xEE, 0xFF, 0x12, 0x34  \
                                                     }

#define DEFAULTKEY_2_VALUE_DEFALUT                   { \
                                                     0x12, 0x22, 0x33, 0x44, \
                                                     0x55, 0x66, 0x77, 0x88, \
                                                     0xAA, 0xBB, 0xCC, 0xDD, \
                                                     0xEE, 0xFF, 0x12, 0x34  \
                                                     }

#define KEY_CODEA_DEFAULT                            { \
                                                     0x0A, 0x22, 0x33, 0x44, \
                                                     0x55, 0x66, 0x77, 0x88, \
                                                     0xAA, 0xBB, 0xCC, 0xDD, \
                                                     0xEE, 0xFF, 0x12, 0x34  \
                                                     }


#define KEY_CODEB_DEFAULT                            { \
                                                     0x0B, 0x22, 0x33, 0x44, \
                                                     0x55, 0x66, 0x77, 0x88, \
                                                     0xAA, 0xBB, 0xCC, 0xDD, \
                                                     0xEE, 0xFF, 0x12, 0x34  \
                                                     }

#define KEY_CODEAB_DEFAULT                           { \
                                                     0xAB, 0x22, 0x33, 0x44, \
                                                     0x55, 0x66, 0x77, 0x88, \
                                                     0xAA, 0xBB, 0xCC, 0xDD, \
                                                     0xEE, 0xFF, 0x12, 0x34  \
                                                     }

#define KEY_BROADCAST_DEFAULT                        { \
                                                     0x67, 0x22, 0x33, 0x44, \
                                                     0x55, 0x66, 0x77, 0x88, \
                                                     0xAA, 0xBB, 0xCC, 0xDD, \
                                                     0xEE, 0xFF, 0x12, 0x34  \
                                                     }

#define KEY_SYSTEM_DEFAULT                          { \
                                                     0x45, 0x22, 0x33, 0x44, \
                                                     0x55, 0x66, 0x77, 0x88, \
                                                     0xAA, 0xBB, 0xCC, 0xDD, \
                                                     0xEE, 0xFF, 0x12, 0x34  \
                                                     }

#define KEY_NVM_DEFAULT                             { \
                                                     0x98, 0x22, 0x33, 0x44, \
                                                     0x55, 0x66, 0x77, 0x88, \
                                                     0xAA, 0xBB, 0xCC, 0xDD, \
                                                     0xEE, 0xFF, 0x12, 0x34  \
                                                     }

#define KEY_NVM_EN_DEFAULT                          { \
                                                     0x98, 0x22, 0x33, 0x44, \
                                                     0x55, 0x66, 0x77, 0x88, \
                                                     0xAA, 0xBB, 0xCC, 0xDD, \
                                                     0xEE, 0xFF, 0x12, 0x34  \
                                                     }

typedef enum eSecureElementStatus
{
    /*!
     * No error occurred
     */
    SECURE_ELEMENT_SUCCESS = 0,
    /*!
     * CCM does not match
     */
    SECURE_ELEMENT_FAIL_CCM,
    /*!
     * Null pointer exception
     */
    SECURE_ELEMENT_ERROR_NPE,
    /*!
     * 无效的key
     */
    SECURE_ELEMENT_ERROR_INVALID_KEY_ID,
    /*!
     * Invalid LoRaWAN specification version
     */
    SECURE_ELEMENT_ERROR_INVALID_LORAWAM_SPEC_VERSION,
    /*!
     * Incompatible buffer size
     */
    SECURE_ELEMENT_ERROR_BUF_SIZE,
    /*!
     * Undefined Error occurred
     */
    SECURE_ELEMENT_ERROR,
    /*!
     * Failed to encrypt
     */
    SECURE_ELEMENT_FAIL_ENCRYPT,
    /*!
     * 不支持
     */
    SECURE_ELEMENT_ERROR_NOT_SUPPORTED,
}SecureElementStatus_t;

/*
 *@note key value定义原则，key[0] != 0xFF, key[1] != 0xFF
 *@note
 *@note KEY_ID_CODEA  RFM每次入网随机产生,HUB通过RFM寻呼获得
 *@note KEY_ID_CODEB  RFM通过入网过程获得,HUB生成机制暂未明确
 *@note KEY_ID_CODEAB 通过入网获得,用于APP消息payload加解密使用
 *@note KEY_ID_DEFAULT_KEY1 生产一次性烧录,用于同网(相同制造商认证鉴权使用)
 *@note KEY_ID_DEFAULT_KEY2 生产一次性烧录,用于同网(相同制造商认证鉴权使用)
 *@note KEY_ID_SYSTEM_KEY_CODE RFM通过入网获得，RFM漫游认证使用
 *@note KEY_ID_BROADCAST_KEY_CODE  RFM通过入网获得,RFM广播数据使用
 *@note
 *@note KEY_ID_NVM_KEY 针对NVM区域的key进行加解密(除了自身的key),通过生产时一次性烧录
 *@note KEY_ID_NVM_KEY_EN_KEY 针对KEY_ID_NVM_KEY进行加解密,通过程序固定
 *
 */
typedef enum {
  KEY_ID_NOKEY = 0,
  KEY_ID_FIRST = 1,
  KEY_ID_CODEA = KEY_ID_FIRST,
  KEY_ID_CODEB,
  KEY_ID_CODEAB,
  KEY_ID_DEFAULT_KEY1,
  KEY_ID_DEFAULT_KEY2,
  KEY_ID_SYSTEM_KEY_CODE,
  KEY_ID_BROADCAST_KEY_CODE,
  KEY_ID_NVM_KEY,
  KEY_ID_NVM_KEY_EN_KEY,
  KEY_ID_LAST = KEY_ID_NVM_KEY,
  KEY_ID_ERROR,
}KeyIdentifier_t;
#define KEY_ID_COUNT                               (KEY_ID_LAST-KEY_ID_FIRST)

typedef enum {
  KEY_EVENT_FIRST = 0,
  //GET
  KEY_EVENT_GET_CODEA = KEY_EVENT_FIRST,
  KEY_EVENT_GET_CODEB,
  KEY_EVENT_GET_CODEAB,
  KEY_EVENT_GET_DEFAULT_KEY1,
  KEY_EVENT_GET_DEFAULT_KEY2,
  KEY_EVENT_GET_SYSTEM_KEY_CODE,
  KEY_EVENT_GET_BROADCAST_KEY_CODE,
  KEY_EVENT_GET_NVM_KEY,
  KEY_EVENT_GET_NVM_KEY_EN_KEY,
  //SET
  KEY_EVENT_SET_CODEA,
  KEY_EVENT_SET_CODEB,
  KEY_EVENT_SET_CODEAB,
  KEY_EVENT_SET_DEFAULT_KEY1,
  KEY_EVENT_SET_DEFAULT_KEY2,
  KEY_EVENT_SET_SYSTEM_KEY_CODE,
  KEY_EVENT_SET_BROADCAST_KEY_CODE,
  KEY_EVENT_SET_NVM_KEY,
  KEY_EVENT_SET_NVM_KEY_EN_KEY,




  KEY_EVENT_LAST = KEY_EVENT_SET_NVM_KEY_EN_KEY,
  KEY_EVENT_ERROR,
}KeyEvent_t;


typedef struct{
  uint8_t key_p[16];                  //明文key
  uint8_t key_c[16];                  //密文key,NVM数据.数据源
  uint16_t keyid;                     //key id
}_key_t;

typedef struct{
  KeyIdentifier_t identifier;
  uint32_t        key_id;
}key_attribute_t;

/*
 *@note key的种类，数量见KeyIdentifier_t描述
 *@note
 *@note
 *
 */
typedef struct{
  key_attribute_t codeA;
  key_attribute_t codeB;
  key_attribute_t codeAB;
  key_attribute_t default_key1;
  key_attribute_t default_key2;
  key_attribute_t system_key_code;
  key_attribute_t broadcast_key_code;
  key_attribute_t nvm_key;
  key_attribute_t nvm_key_en_key;

  key_attribute_t aes_key;
  uint8_t use_count;

}key_group_t;

/*
 *@note: 随机生成codeA
 *
 */
SecureElementStatus_t RBF_generate_data_codeA(uint8_t *key);
/*
 *@brief  获取codeA
 *@param
 *@param
 *
 *
 */
SecureElementStatus_t RBF_get_data_codeA (uint8_t *key);
/*
 *@brief  设置codeA
 *@param
 *@param
 *
 *
 */
void RBF_set_data_codeA(uint8_t *key);

/*
 *@note: HUB随机生成codeB，注意：只在网络初始化的情况下进行，
 *@note: TODO:前期先针对NodeJoinInfo数量为0时进行初始化。是否会存在其它情况？
 *
 *
 */
SecureElementStatus_t RBF_generate_data_codeB(uint8_t *key);
/*
 *@brief  获取codeB
 *@param
 *@param
 *
 *
 */
SecureElementStatus_t RBF_get_data_codeB (uint8_t *key);
/*
 *@brief  设置codeB
 *@param
 *@param
 * *
 */
void RBF_set_data_codeB(uint8_t *key);
/*
 *@brief  获取codeAB
 *@param
 *@param
 *
 *
 */
SecureElementStatus_t RBF_get_data_codeAB (uint8_t *key);
/*
 *@brief  设置codeAB
 *@param
 *@param
 * *
 */
void RBF_set_data_codeAB(uint8_t *key);
/*
 *@brief  获取systemKeyCode
 *@param
 *@param
 *
 *
 */
SecureElementStatus_t RBF_get_data_systemKeyCode (uint8_t *key);
/*
 *@brief  设置systemKeyCode
 *@param
 *@param
 * *
 */
void RBF_set_data_systemKeyCode(uint8_t *key);
/*
 *@brief  获取broadcastKeyCode
 *@param
 *@param
 *
 *
 */
SecureElementStatus_t RBF_get_data_broadcastKeyCode (uint8_t *key);
/*
 *@brief  设置broadcastKeyCode
 *@param
 *@param
 * *
 */
void RBF_set_data_broadcastKeyCode(uint8_t *key);
/*
 * 获取defaultKey1
 *
 */
void RBF_get_data_defaultKey1(uint8_t *key);
/*
 * 该Key用于系统一次性烧录，整个网络的设备必须统一,应用中不提供set接口。
 *
 *
 */
SecureElementStatus_t RBF_set_data_defaultKey1(uint8_t *key);
/*
 *
 *
 */
void RBF_get_data_defaultKey2(uint8_t *key);
/*
 *  该Key用于系统一次性烧录，整个网络的设备必须统一,应用中不提供set接口。
 *
 */
SecureElementStatus_t RBF_set_data_defaultKey2(uint8_t *key);

SecureElementStatus_t RBF_get_data_nvmKey (uint8_t *key);
void RBF_set_data_nvmKey(uint8_t *key);
SecureElementStatus_t RBF_get_key_id (KeyIdentifier_t identifier,uint32_t * key_id);

uint32_t RBF_get_keyid_codeA(void);
uint32_t RBF_get_keyid_codeB(void);
uint32_t RBF_get_keyid_codeAB(void);
uint32_t RBF_get_keyid_systemKeyCode(void);
uint32_t RBF_get_keyid_broadcastKeyCode(void);
uint32_t RBF_get_keyid_defaultKey1(void);
uint32_t RBF_get_keyid_defaultKey2(void);
uint32_t RBF_get_keyid_nvmkey(void);

//void RBF_set_keyid_defaultKey1(uint8_t *key);
//void RBF_set_keyid_defaultKey2(uint8_t *key);
//void RBF_set_keyid_codeA(uint8_t *key);
//void RBF_set_keyid_codeB(uint8_t *key);
//void RBF_set_keyid_codeAB(uint8_t *key);

/***
 *
 * @brief 随机生成一个16B key
 * @param key
 * @return
 */
SecureElementStatus_t RBF_generate_data_key(uint8_t *key);
void RBF_keyNVM_init (void);

void RBF_key_group_info_log(void);


void RBF_keyNVM_test (void);

#endif /* MAC_RBFKEYNVM_H_ */
